package com.lujianfei.productprice.ui.addProduct;

import android.os.Bundle;
import android.widget.EditText;

import com.lujianfei.productprice.R;
import com.lujianfei.productprice.base.BaseActivity;

public class AddProductActivity extends BaseActivity implements AddProductContract.View {

    private AddProductContract.Presenter presenter;
    private EditText editProductName;
    private EditText editProductPrice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_product);
        setTitleMiddleText(-1 == getId()?"添加产品":"编辑产品");
        setTitleRightImage(R.drawable.ic_ok);
        showTitleRightImage();
        editProductName = findViewById(R.id.editProductName);
        editProductPrice = findViewById(R.id.editProductPrice);
        presenter = new AddProductPresenter(this);
        presenter.loadData();
    }

    @Override
    public void onTitleRightImageClick() {
        super.onTitleRightImageClick();
        presenter.save();
        finish();
    }

    @Override
    protected int getTitleBarResId() {
        return R.id.custom_titlebar;
    }

    @Override
    public long getId() {
        return getIntent().getLongExtra("id",-1);
    }

    @Override
    public void setData(ProductEntity productEntity) {
        if (null == productEntity){
            return;
        }
        editProductName.setText(productEntity.getProductName());
        editProductPrice.setText(String.format("%.2f",productEntity.getPrice()));
    }

    @Override
    public String getProductName() {
        return editProductName.getText().toString();
    }

    @Override
    public String getProductPrice() {
        return editProductPrice.getText().toString();
    }

    @Override
    public long getShopId() {
        return getIntent().getLongExtra("shopId",-1);
    }
}
